package com.mzba.easy.weibo.ui;

import java.lang.reflect.Field;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.mzba.easy.weibo.R;
import com.mzba.easy.weibo.db.EmotionField;
import com.mzba.easy.weibo.util.AppContext;
/**
 * 
 * @author 06peng
 *
 */
public class GridViewAdapter extends BaseAdapter {

	public List<EmotionField> emotions;
	private Context context;
	
	public GridViewAdapter(Context context){
		this.emotions = AppContext.getInstance().getEmotionFields();
		this.context = context;
	}
	
	@Override
	public int getCount() {
		return emotions.size();
	}

	@Override
	public Object getItem(int position) {
		return emotions.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item, null);
			holder.imageView = (ImageView) convertView.findViewById(R.id.image);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			Field f = emotions.get(position).getField();
			int resId = f.getInt(R.drawable.class);	
			holder.imageView.setImageResource(resId);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return convertView;
	}
	
	public static class ViewHolder {
		ImageView imageView;
	}
}
